﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using NUnit.Framework;

//namespace ValidationFramework.Tests.Integration
//{


//    [TestFixture]
//    public class XmlConfigurationTests
//    {

//        private const string propertyXmlString = @"<?xml version='1.0' encoding='utf-8' ?>
//<validationMapping xmlns='urn:validationFramework-validationDefinition-1.5'>
//  <class typeName='ValidationFramework.Tests.Configuration.Person, ValidationFramework.Tests'>
//    <property name='Name'>
//      <rule typeName='RequiredStringRule' initialValue='aaaa'/>
//    </property>
//    <property name='Age'>
//      <rule typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//    </property>
//  </class>
//</validationMapping>";

//        private const string methodXmlString = @"<?xml version='1.0' encoding='utf-8' ?>
//<validationMapping xmlns='urn:validationFramework-validationDefinition-1.5'>
//  <class typeName='ValidationFramework.Tests.Configuration.Person, ValidationFramework.Tests'>
// 
//    <method name='SetTheAge'>
//      <overloadType typeName='System.Int32'/>
//      <parameter name='age'>
//        <rule typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//        <rule ruleSet='a' typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//        <rule ruleSet='b' typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//        <rule ruleInterpretation='This is a custom rule' ruleSet='foo' validationMethod='NameIsDane' validationTypeName='ValidationFramework.Tests.Configuration.Person,ValidationFramework.Tests' errorMessage='hello' typeName='CustomRule'/>
//      </parameter>
//    </method>
//    <method name='SetTheAge2'>
//      <parameter name='age'>
//        <rule typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//        <rule ruleSet='a' typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//        <rule ruleSet='b' typeName='RangeRule' minimum='18' maximum='80' errorMessage='Age is invalid' />
//        <rule ruleInterpretation='This is a custom rule' ruleSet='foo' validationMethod='NameIsDane' validationTypeName='ValidationFramework.Tests.Configuration.Person,ValidationFramework.Tests' errorMessage='hello' typeName='CustomRule'/>
//      </parameter>
//    </method>
//  </class>
//</validationMapping>";
//        public class AddAssembly : ConfigurationServiceTestBase
//        {
//            [Test]
//            public void ByAssembly()
//            {
//                _xmlService.AddAssembly(typeof(XmlConfigurationServiceTest).Assembly);
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }


//            [Test]
//            public void ByName()
//            {
//                _xmlService.AddAssembly("ValidationFramework.Tests");
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);


//            }


//            [Test]
//            [ExpectedException(typeof(FileNotFoundException))]
//            public void ExceptionInvalidName()
//            {
//                _xmlService.AddAssembly("InvalidAssemblyName");
//            }

//        }

//        public class AddClass : ConfigurationServiceTestBase
//        {
//            [Test]
//            public void Simple()
//            {
//                var type = typeof(Person);
//                _xmlService.AddClass(type);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }

//        }

//        public class AddXmlFile : ConfigurationServiceTestBase
//        {
//            [Test]
//            [ExpectedException(typeof(Exception))]
//            public void ExceptionNotExist()
//            {
//                _xmlService.AddXmlFile(@"Invalid.xml");
//            }

//            [Test]
//            public void Simple()
//            {
//                var path = WriteXmlConfig();

//                _xmlService.AddXmlFile(path);
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }


//        }
//        private static string WriteXmlConfig()
//        {
//            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ValidationFramework.Tests.Configuration.Person.validation.xml"))
//            {
//                using (var streamReader = new StreamReader(stream))
//                {
//                    var fullPath = Path.GetFullPath("temp.xml");
//                    using (var streamWriter = new StreamWriter(fullPath, false))
//                    {
//                        streamWriter.Write(streamReader.ReadToEnd());
//                    }
//                    return fullPath;
//                }
//            }
//        }

//        public class AddUrl : ConfigurationServiceTestBase
//        {
//            [Test]
//            public void AddUrlValid()
//            {
//                var path = WriteXmlConfig();
//                _xmlService.AddUrl(path);
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }


//            [Test]
//            public void AddUriValid()
//            {

//                var path = WriteXmlConfig();
//                _xmlService.AddUrl(new Uri(path));
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }
//        }

//        public class AddDirectory : ConfigurationServiceTestBase
//        {

//            [Test]
//            public void Simple()
//            {
//                try
//                {
//                    var path = WriteXmlConfig();
//                    Directory.CreateDirectory("temp");
//                    File.Move(path, "temp/temp.validation.xml");
//                    _xmlService.AddDirectory(new DirectoryInfo(@"./"));
//                    var type = typeof(Person);
//                    var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                    Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                    var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                    var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                    Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                    var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                    var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                    Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//                }
//                finally
//                {
//                    if (Directory.Exists("temp"))
//                    {
//                        if (File.Exists("temp/temp.validation.xml"))
//                        {
//                            File.Delete("temp/temp.validation.xml");
//                            Directory.Delete("temp");
//                        }
//                    }
//                }

//            }
//        }

//        public class AddXmlString : ConfigurationServiceTestBase
//        {

//            [Test]
//            public void Method()
//            {
//                _xmlService.AddXmlString(methodXmlString);
//                var type = typeof(Person);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }


//            [Test]
//            public void Property()
//            {
//                _xmlService.AddXmlString(propertyXmlString);
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(1, typeDescriptor.Properties["Age"].RuleCount);
//            }


//            [Test]
//            public void PropertySub()
//            {
//                _xmlService.AddXmlString(propertyXmlString);
//                var type = typeof(Employee);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(1, typeDescriptor.Properties["Age"].RuleCount);
//            }

//        }
//#if (!SILVERLIGHT)
//        public class AddDocument : ConfigurationServiceTestBase
//        {
//            [Test]
//            public void Method()
//            {
//                var xmlDocument = new XmlDocument();
//                xmlDocument.LoadXml(methodXmlString);
//                _xmlService.AddDocument(xmlDocument);
//                var type = typeof(Person);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }


//            [Test]
//            public void Property()
//            {
//                var xmlDocument = new XmlDocument();
//                xmlDocument.LoadXml(propertyXmlString);
//                _xmlService.AddDocument(xmlDocument);
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(1, typeDescriptor.Properties["Age"].RuleCount);
//            }

//        }
//#endif
//        public class AddXmlReader : ConfigurationServiceTestBase
//        {
//            [Test]
//            public void Property()
//            {
//                var xmlTextReader = XmlReader.Create(new StringReader(propertyXmlString));
//                _xmlService.AddXmlReader(xmlTextReader);
//                var type = typeof(Person);
//                var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                Assert.AreEqual(1, typeDescriptor.Properties["Age"].RuleCount);

//            }


//            [Test]
//            public void Method()
//            {
//                var xmlTextReader = XmlReader.Create(new StringReader(methodXmlString));
//                _xmlService.AddXmlReader(xmlTextReader);
//                var type = typeof(Person);

//                var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//            }
//        }

//        public class AddInputStream : ConfigurationServiceTestBase
//        {

//            [Test]
//            public void Simple()
//            {
//                using (var stream = GetType().Assembly.GetManifestResourceStream("ValidationFramework.Tests.Configuration.Person.validation.xml"))
//                {
//                    _xmlService.AddInputStream(stream);
//                    var type = typeof(Person);
//                    var typeDescriptor = TypeCache.GetType(type.TypeHandle);
//                    Assert.AreEqual(4, typeDescriptor.Properties["Age"].RuleCount);

//                    var setTheAgeIntMethodHandle = type.GetMethod("SetTheAge", new[] { typeof(int) }).MethodHandle;
//                    var intSetTheAgeMethodDescriptor = MethodCache.GetMethod(setTheAgeIntMethodHandle);
//                    Assert.AreEqual(4, intSetTheAgeMethodDescriptor.Parameters["age"].RuleCount);

//                    var setTheAge2MethodHandle = type.GetMethod("SetTheAge2").MethodHandle;
//                    var setTheAge2MethodDescriptor = MethodCache.GetMethod(setTheAge2MethodHandle);
//                    Assert.AreEqual(4, setTheAge2MethodDescriptor.Parameters["age"].RuleCount);
//                }
//            }

//        }
//        [TestFixture]
//        public class GetRule : ConfigurationServiceTestBase
//        {
//            [Test]
//            [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Could not load IRuleConfigReader type. Tried 'ValidationFramework.Configuration.InvalidReaderConfigReader,ValidationFramework'.")]
//            public void ExceptionNotExistNoCommas()
//            {
//                const string validatorXml = "<rule validationExpression='aaaaa' typeName='InvalidReader'/>";
//                var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//                _xmlService.GetRule(ruleData, new FakeInfoDescriptor());
//            }

//            [Test]
//            [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Could not load IRuleConfigReader type. Tried 'AssemblyName,InvalidReader'.")]
//            public void ExceptionNotExistCommas()
//            {
//                const string validatorXml = "<rule validationExpression='aaaaa' typeName='AssemblyName,InvalidReader'/>";
//                var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//                _xmlService.GetRule(ruleData, new FakeInfoDescriptor());
//            }
//#if (WindowsCE)
//                    [Test]
//                    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Could not load IRuleConfigReader type. Tried 'CompactValidationFramework.Tests.Configuration.NoConfigReaderRuleConfigReader,ValidationFramework.Tests, Version=1.1.0.0, Culture=neutral, PublicKeyToken=2f07bd240683f08a'.")]
//                    public void ExceptionNoConfigReader()
//                    {
//                        const string validatorXml = "<rule validationExpression='aaaaa' typeName='ValidationFramework.Tests.Configuration.NoConfigReaderRule,CompactValidationFramework.Tests'/>";
//                    var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//                        XmlConfigurationService.GetRule(ruleData, TypePointers.StringTypeHandle);
//                    }

//#else
//            [Test]
//            [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Could not load IRuleConfigReader type. Tried 'ValidationFramework.Tests.Configuration.NoConfigReaderRuleConfigReader,ValidationFramework.Tests, Version=1.1.0.0, Culture=neutral, PublicKeyToken=2f07bd240683f08a'.")]
//            public void ExceptionNoConfigReader()
//            {
//                const string validatorXml = "<rule validationExpression='aaaaa' typeName='ValidationFramework.Tests.Configuration.NoConfigReaderRule,ValidationFramework.Tests'/>";
//                var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//                _xmlService.GetRule(ruleData, new FakeInfoDescriptor());
//            }

//#endif
//            // UNDONE: Configuration Service doesn't need to ehck for valid regex validators
//            //[Test]
//            //public void Simple1()
//            //{
//            //    const string validatorXml = "<rule validationExpression='aaaaa' errorMessage='hello' typeName='ValidationFramework.Configuration.RegexRuleConfigReader,ValidationFramework' ruleSet='foo' regexOptions='RightToLeft'/>";
//            //    var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//            //    var rule = (RegexValidator)ConfigurationService.GetRule(ruleData, new FakeInfoDescriptor());
//            //    Assert.AreEqual("hello", rule.ErrorMessage);
//            //    Assert.AreEqual(RegexOptions.RightToLeft, rule.RegexOptions);
//            //    Assert.AreEqual("aaaaa", rule.ValidationExpression);
//            //}
//            // UNDONE: Configuration Service doesn't need to ehck for valid regex validators
//            //[Test]
//            //public void Simple2()
//            //{
//            //    const string validatorXml = "<rule validationExpression='aaaaa' errorMessage='hello' typeName='RegexRuleConfigReader' ruleSet='foo' regexOptions='RightToLeft'/>";
//            //    var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//            //    var rule = (RegexValidator)ConfigurationService.GetRule(ruleData, new FakeInfoDescriptor());
//            //    Assert.AreEqual("hello", rule.ErrorMessage);
//            //    Assert.AreEqual(RegexOptions.RightToLeft, rule.RegexOptions);
//            //    Assert.AreEqual("aaaaa", rule.ValidationExpression);
//            //}
//            // UNDONE: Configuration Service doesn't need to ehck for valid regex validators
//            //[Test]
//            //public void Simple3()
//            //{
//            //    const string validatorXml = "<rule validationExpression='aaaaa' errorMessage='hello' typeName='RegexRule' ruleSet='foo' regexOptions='RightToLeft'/>";
//            //    var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//            //    var rule = ConfigurationService.GetRule(ruleData, new FakeInfoDescriptor());
//            //    Assert.AreEqual("hello", rule.ErrorMessage);
//            //    Assert.AreEqual(RegexOptions.RightToLeft, rule.RegexOptions);
//            //    Assert.AreEqual("aaaaa", rule.ValidationExpression);
//            //}

//            // UNDONE: This is a strange test.
//            //[Test]
//            //public void Simple4()
//            //{
//            //    const string validatorXml = "<rule errorMessage='hello' typeName='ValidationFramework.Tests.MockRule,ValidationFramework.Tests' ruleSet='foo'/>";
//            //    var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//            //    var rule = ConfigurationService.GetRule(ruleData, new FakeInfoDescriptor());
//            //    Assert.AreEqual("hello", rule.ErrorMessage);
//            //}
//            // UNDONE: This is a strange test.
//            //[Test]
//            //public void Simple5()
//            //{
//            //    const string validatorXml = "<rule errorMessage='hello' typeName='ValidationFramework.Tests.MockRuleConfigReader,ValidationFramework.Tests' ruleSet='foo'/>";
//            //    var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
//            //    var rule = ConfigurationService.GetRule(ruleData, new FakeInfoDescriptor());
//            //    Assert.AreEqual("hello", rule.ErrorMessage);
//            //}
//        }
//    }
//}
